package me.eachann.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import me.eachann.widget.segmented.control.R;

/**
 * 分段控件
 * created by lijian on 2019/12/17
 */
public class UISegmentedControl extends RadioGroup {
    private ColorStateList csl;

    public UISegmentedControl(Context context) {
        this(context, null);
    }

    public UISegmentedControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        csl = getResources().getColorStateList(R.color.selector_segmented_control_text_color);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity=Gravity.CENTER;
        setLayoutParams(params);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() == 0) {
            return;
        }
        if (getChildCount() == 1) {
            getChildAt(0).setBackgroundResource(R.drawable.drawable_segment_control_bg_single);
        }
        if (getChildCount() == 2) {
            getChildAt(0).setBackgroundResource(R.drawable.drawable_segment_control_bg_item_start);
            getChildAt(1).setBackgroundResource(R.drawable.drawable_segment_control_bg_item_end);
        }
        if (getChildCount() > 2) {
            getChildAt(0).setBackgroundResource(R.drawable.drawable_segment_control_bg_item_start);
            for (int i = 1; i < getChildCount() - 1; i++) {
                getChildAt(i).setBackgroundResource(R.drawable.drawable_segment_control_bg_item_middle);
            }
            getChildAt(getChildCount() - 1).setBackgroundResource(R.drawable.drawable_segment_control_bg_item_end);
        }
        if (getChildAt(0) instanceof RadioButton) {
            ((RadioButton) getChildAt(0)).setChecked(true);
        }
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioButton) {
                RadioGroup.LayoutParams params = (LayoutParams) getChildAt(i).getLayoutParams();
                params.width=0;
                params.weight = 1;
                ((RadioButton) getChildAt(i)).setButtonDrawable(null);
                ((RadioButton) getChildAt(i)).setTextColor(csl);
                ((RadioButton) getChildAt(i)).setTextSize(13);
                ((RadioButton) getChildAt(i)).setGravity(Gravity.CENTER);
                ((RadioButton) getChildAt(i)).setSingleLine(true);
                ((RadioButton) getChildAt(i)).setEllipsize(TextUtils.TruncateAt.MIDDLE);
                getChildAt(i).setLayoutParams(params);
            }
        }
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        switch (MeasureSpec.getMode(widthMeasureSpec)) {
//            case MeasureSpec.AT_MOST:
//            case MeasureSpec.UNSPECIFIED:
//                widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
//                break;
//            case MeasureSpec.EXACTLY:
//            default:
//                break;
//        }
//        if (getChildCount() == 1) {
//            widthMeasureSpec = MeasureSpec.makeMeasureSpec(dp2px(190f), MeasureSpec.EXACTLY);
//        } else if (getChildCount() >= 2) {
//            widthMeasureSpec = MeasureSpec.makeMeasureSpec(dp2px(350f), MeasureSpec.EXACTLY);
//        }
//
//        switch (MeasureSpec.getMode(heightMeasureSpec)) {
//            case MeasureSpec.AT_MOST:
//            case MeasureSpec.UNSPECIFIED:
//                final Drawable background = getBackground();
//                // 如果当前背景是一张图片的话
//                if (background instanceof BitmapDrawable) {
//                    // 算出标题栏的宽度和图片的宽度之比例
//                    final double ratio = (double) MeasureSpec.getSize(widthMeasureSpec) / (double) background.getIntrinsicWidth();
//                    heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (ratio * background.getIntrinsicHeight()), MeasureSpec.EXACTLY);
//                } else {
//                    // 获取 TitleBar 默认高度
//                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(dp2px(30f), MeasureSpec.EXACTLY);
//                }
//                break;
//            case MeasureSpec.EXACTLY:
//            default:
//                break;
//        }
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//    }

    private int dp2px(float dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
    }
}
